package cn.lingyangwl.agile.food.core.service.impl;

import cn.lingyangwl.agile.food.core.assembly.StockDocumentAssembly;
import cn.lingyangwl.agile.food.core.manager.StockDocumentManager;
import cn.lingyangwl.agile.food.core.mapper.StockDocumentMapper;
import cn.lingyangwl.agile.food.model.rqrs.sku.SkuInOutRecordResp;
import cn.lingyangwl.agile.food.core.service.StockDocumentService;
import cn.lingyangwl.agile.food.model.entity.StockDocument;
import cn.lingyangwl.agile.food.model.rqrs.sku.SkuInOutRecordPageReq;
import cn.lingyangwl.agile.food.model.rqrs.sku.SkuInOutRecordSaveReq;
import cn.lingyangwl.agile.food.model.rqrs.sku.SkuInOutRecordUpdateReq;
import cn.lingyangwl.framework.mybatis.plus.utils.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 库存单据记录
 *
 * @author shenguangyang
 */
@Slf4j
@Service
public class StockDocumentServiceImpl extends ServiceImpl<StockDocumentMapper, StockDocument>
    implements IService<StockDocument>, StockDocumentService {

    @Resource
    private StockDocumentManager stockDocumentManager;
    @Resource
    private StockDocumentAssembly stockDocumentAssembly;
    
    @Override
    public IPage<SkuInOutRecordResp> pageRecord(SkuInOutRecordPageReq req) {
        LocalDateTime beginTime = req.getBeginLocalDateTime();
        LocalDateTime endTime = req.getEndLocalDateTime();
        LambdaQueryWrapper<StockDocument> lqw = StockDocument.lqw()
            .eq(StringUtils.isNotEmpty(req.getStockNo()), StockDocument::getStockNo, req.getStockNo())
            .eq(StringUtils.isNotEmpty(req.getStatus()), StockDocument::getStatus, req.getStatus())
            .ge(Objects.nonNull(beginTime), StockDocument::getCreatedAt, beginTime)
            .le(Objects.nonNull(endTime), StockDocument::getCreatedAt, endTime)
            .orderByDesc(StockDocument::getCreatedAt);
        IPage<StockDocument> pageData = this.page(PageUtil.to(req), lqw);
        return PageUtil.to(pageData, stockDocumentAssembly::toSkuStockInOutRecordResp);
    }
    
    @Override
    public List<SkuInOutRecordResp> listRecord() {
    	return this.list().stream()
    	        .map(stockDocumentAssembly::toSkuStockInOutRecordResp).collect(Collectors.toList());
    }

    @Override
    public SkuInOutRecordResp getRecord(Long id) {
        StockDocument entity = this.getById(id);
    	return stockDocumentAssembly.toSkuStockInOutRecordResp(entity);
    }

    @Override
    public boolean saveRecord(SkuInOutRecordSaveReq req) {
        StockDocument entity = stockDocumentAssembly.toSkuStockInOutRecord(req);
    	return this.save(entity);
    }

    @Override
    public boolean updateRecord(SkuInOutRecordUpdateReq req) {
        StockDocument entity = stockDocumentAssembly.toSkuStockInOutRecord(req);
    	return this.updateById(entity);
    }

    @Override
    public boolean deleteRecord(Long id) {
    	return this.removeById(id);
    }
    
    @Override
    public boolean batchDeleteRecord(List<Long> ids) {
    	return this.removeByIds(ids);
    }
}
